import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
// import Home from '../views/home'
import Login from '../views/login'
import Layout from '../views/layout'
import local from '@/utils/local'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}



const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  //首页
  {
    path: '/home', meta: { path: '/home', title: '首页' }, component: Layout, children: [
      { path: "", meta: { path: '/home', title: '首页' }, component: () => import("../views/home") }
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})


// 声明一个数组，数组中记录了需要计算的所有路由
const jisuanRouter = [

  { path: '/', redirect: '/login', meta: { isMenu: false } },
  { path: '/login', component: Login, meta: { isMenu: false } },

  //首页
  {
    path: '/home', meta: { path: '/home', title: '首页', icon: "icon-31shouyexuanzhong", isMenu: true }, component: Layout, children: [
      { path: "", meta: { path: '', title: '首页', isMenu: true }, component: () => import("../views/home") }
    ]
  },

  // 订单
  // super和normal都可以访问的时候 可以不加roles
  {
    path: '/order', meta: { level: 2, path: '/order', title: '订单管理', icon: "el-icon-s-order", isMenu: true }, component: Layout, children: [
      // { path: "", meta: { path: '', title: '订单管理' }, redirect: '/order/list' },
      { path: 'list', meta: { path: '/order/list', title: '订单列表', isMenu: true }, component: () => import('../views/order/List.vue') },
      { path: 'detail', meta: { path: '/order/detail', title: '订单详情', isMenu: false }, component: () => import('../views/order/Detail.vue') },
      { path: 'update', meta: { path: '/order/update', title: '订单修改', isMenu: false }, component: () => import('../views/order/Update.vue') },
    ]
  },
  // 用户
  {
    path: '/users', meta: { level: 2, path: '/users', title: '用户管理', roles: ['super', 'normal'], icon: "el-icon-user-solid", isMenu: true }, redirect: '/users/list', component: Layout, children: [
      // { path: "", meta: { path: '/users', title: '用户管理' }, redirect: '/users/list' },
      { path: 'list', meta: { path: '/users/list', title: '用户列表', roles: ['super'], isMenu: true }, component: () => import('../views/users/list.vue') },
      { path: 'save', meta: { path: '/users/save', title: '添加账号', roles: ['super'], isMenu: true }, component: () => import('../views/users/save.vue') },
      { path: 'userpwd', meta: { path: '/users/userpwd', title: '修改密码', roles: ['super', 'normal'], isMenu: true }, component: () => import('../views/users/userpwd.vue') },
      { path: 'personal', meta: { path: '/users/personal', title: '个人中心', roles: ['super', 'normal'], isMenu: true }, component: () => import('../views/users/personal.vue') },
    ]
  },
  // goods
  {
    path: '/goods', meta: { level: 2, path: '/goods', title: '商品管理', roles: ['super', 'normal'], icon: "el-icon-s-goods", isMenu: true }, redirect: '/goods/list', component: Layout, children: [
      // { path: '', meta: { path: '', title: '商品管理' }, redirect: '/goods/list' },
      { path: 'list', meta: { path: '/goods/list', title: '商品列表', roles: ['super', 'normal'], isMenu: true }, component: () => import('../views/goods/GoodsList.vue') },
      { path: 'save', meta: { path: '/goods/save', title: '添加商品', roles: ['super', 'normal'], isMenu: true }, component: () => import('../views/goods/GoodsSave.vue') },
      { path: 'goodstype', meta: { path: '/goods/goodstype', title: '商品分类', roles: ['super'], isMenu: true }, component: () => import('../views/goods/GoodsType.vue') }
    ]
  },
  //shops
  {
    path: "/shop", meta: { level: 1, path: "/shop", title: "店铺管理", roles: ['super'], icon: "el-icon-house", isMenu: true }, component: Layout, children: [
      { path: "", meta: { path: "", title: "店铺管理", roles: ['super'], isMenu: true }, component: () => import("../views/shop") }
    ]
  },

  //census
  {
    path: "/census", meta: { level: 2, path: "/census", title: "统计管理", roles: ['super'], icon: "el-icon-finished", isMenu: true }, component: Layout, redirect: "/census/goods", children: [
      { path: "goods", meta: { path: "/census/goods", title: "商品统计", roles: ['super'], isMenu: true }, component: () => import("../views/census/GoodsCensus.vue") },
      { path: "order", meta: { path: "/census/order", title: "订单统计", roles: ['super'], isMenu: true }, component: () => import("../views/census/OrderCensus.vue") }
    ]
  },
  {path:'/404',component:()=>import('../views/404')},
  {path:'*',redirect:'/404'},
]

// 只有两个角色 一个为super 一个为normal

export function jisuan() {
  let role=local.get('roles')
  let arr = []
  jisuanRouter.forEach((item, index) => {
    if (isRoles(item, role)) {
      let chs = []
      if (item.children) {
        item.children.forEach((obj) => {
          //这的obj为children里面的二级对象
          if (isRoles(obj, role)) {
            chs.push(obj)
          }
        })
      }
      item.children = chs
      arr.push(item)
    }
  })
     // 把arr添加到配置里面
  router.addRoutes(arr)

  // console.log(arr);

  // 对arr进行二次处理  把bbs添加渲染到目录里面
  let bbs = [];
  arr.forEach((item) => {
    if (isMenu(item)) {
      if (item.children) {
        let aas = []
        item.children.forEach((item) => {
          if (isMenu(item)) {
            aas.push(item);
          }
        })
        item.children = aas;
      }
      bbs.push(item)
    }
  })
  // 将arr添加到本地
  local.set('menuList', bbs)
}

jisuan()


// 定义一个判断当前路由中的meta对象中的roles的数组中是否包含现在这个角色  包含就可以看
// 注意 如果没有roles 则表示都可以看
function isRoles(obj, role) {
  if (obj.meta && obj.meta.roles) {
    // 判断当前的roles是否包含role
    return obj.meta.roles.includes(role)
  } else {
    return true
  }
}


// 排除目录的函数
function isMenu(obj) {
  if (obj.meta && obj.meta.isMenu) {
    return true
  } else {
    return false
  }
}





// 全局前置导航首位，进入任何一个导航路由时 都会进入守卫
router.beforeEach((to, from, next) => {
  console.log(to, from);
  if (to.path == '/login') {
    next()
  } else {
    if (local.isKey('sell_token')) {
      next()
    } else {
      next('/login')
    }
  }
})

export default router
